package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.*;
import org.tis.tools.abf.module.ac.entity.AcOperator;
import org.tis.tools.abf.module.ac.service.IAcOperatorService;
import org.tis.tools.core.utils.StringUtil;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.auth.TisOperator;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acOperator的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acOperators")
@Slf4j
public class AcOperatorController extends BaseController<AcOperator>  {

    @Autowired
    private IAcOperatorService acOperatorService;


    @ApiOperation(value = "新增操作员",notes = "传入AcOperatorAddRequest作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增操作员")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "AcOperatorAddRequest",name = "传入的AcOperatorAddRequest类",required = true) AcOperatorAddRequest request) {

        if (acOperatorService.existAddUserId(request.getUserId())){
            return ResultVO.error("404","登录用户名已存在,请重新输入!");
        }

         acOperatorService.addAcOperator(request);
        return ResultVO.success("新增成功!");
    }


    @ApiOperation(value = "修改操作员",notes = "传入AcOperator作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改操作员")
    @PutMapping
    public ResultVO update(@RequestBody @Validated({AcOperatorUpdateGrop.class}) @ApiParam(value = "AcOperator",name = "传入的AcOperator类",required = true) AcOperator acOperator) {

        if (StringUtil.isEmpty(acOperator.getUserId())){
            return ResultVO.error("404","登录用户名不能为空!");
        }
        if (StringUtil.isEmpty(acOperator.getGuid())){
            return ResultVO.error("404","GUID不能为空!");
        }
        if (acOperatorService.existUpdateUserId(acOperator.getUserId(),acOperator.getGuid())){
            return ResultVO.error("404","登录用户名已存在,请重新输入!");
        }

        acOperator = acOperatorService.updateAcOperatorByCondition(acOperator);
        return ResultVO.success("修改成功！",acOperator);
    }


    @ApiOperation(value = "删除操作员结果",notes = "传入操作员的GUID作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除操作员")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "操作员的GUID",required = true) String id) {
        AcOperator acOperator  = acOperatorService.selectById(id);
        if (acOperator == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acOperatorService.moveOperator(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "批量删除操作员",notes = "传入AcDeleteListRequest类作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "批量删除操作员")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "AcDeleteListRequest",name = "传入的AcDeleteListRequest类",required = true) AcDeleteListRequest listRequest) {

        acOperatorService.batchDelete(listRequest);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据ID查询操作员",notes = "传入操作员的GUID作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "操作员的GUID",required = true) String id) {
        AcOperator acOperator = acOperatorService.selectById(id);
        if (acOperator == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acOperator);
    }


    @ApiOperation(value = "根据USERID查询操作员",notes = "传入操作员的UserId作为参数")
    @GetMapping("/auth/{userId}")
    public ResultVO detailByUserId(@PathVariable @NotBlank(message = "userId不能为空") @ApiParam(value = "userId",name = "操作员的UserId", required = true) String userId) {
        AcOperator acOperator = acOperatorService.queryByUserId(userId);
        if (acOperator == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        TisOperator tisOperator = new TisOperator();
        BeanUtils.copyProperties(acOperator, tisOperator);
        return ResultVO.success("查询成功", tisOperator);
    }


    @ApiOperation(value = "分页查询操作员",notes = "传入分页page作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "请求的page和condition", required = true) SmartPage<AcOperator> page) {
        return  ResultVO.success("查询成功", acOperatorService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "不分页查询所有操作员",notes = "不需要传入参数")
    @GetMapping("/queryAllOperator")
    public ResultVO queryAllOperator(){
        return ResultVO.success("查询成功",acOperatorService.queryAllOperator());
    }


    @ApiOperation(value = "改变操作员状态",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="改变操作员状态")
    @PutMapping("/changeStatus")
    public ResultVO changeOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("修改成功!",acOperatorQue);
    }

    @ApiOperation(value = "根据条件查询角色下的操作员(分页)", notes = "角色ID(roleId)和分页信息为参数")
    @PostMapping("/queryByRole/{roleId}")
    public ResultVO queryOperatorByRole(@RequestBody @Validated @ApiParam(value = "page",name = "请求的page和condition", required = true) SmartPage<AcOperator> page,@PathVariable @NotBlank(message = "roleId不能为空") @ApiParam(value = "roleId",name = "角色的GUID", required = true) String roleId){
        return ResultVO.success("查询成功",acOperatorService.queryByRole(roleId,page.getPageObj(),page.getConditionWrapper()));
    }

    @OperateLog(type = OperateType.UPDATE,desc = "修改操作员密码")
    @ApiOperation(value = "修改操作员密码", notes = "操作员guid和新密码都需要传")
    @PutMapping("/changePassword")
    public ResultVO changePassword(@RequestBody @Validated({AcOperatorUpdateGrop.class}) @ApiParam(name = "acOpertor"
            ,value = "操作员对象", required = true) AcOperator acOperator){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperator.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("密码修改成功",acOperatorService.changePassword(acOperator));
    }

    @OperateLog(type = OperateType.UPDATE,desc = "重置操作员密码")
    @ApiOperation(value = "重置操作员密码", notes = "操作员guid需要传")
    @PutMapping("/resetPassword/{id}")
    public ResultVO resetPassword(@PathVariable @NotBlank @ApiParam(name = "id",value = "操作员GUID", required = true) String id){
        AcOperator acOperatorQue  = acOperatorService.selectById(id);
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("密码重置成功",acOperatorService.resetPassword(id));
    }

    @ApiOperation(value = "员工入职时绑定操作员,查询所有未被绑定的操作员",notes = "不需要传参")
    @GetMapping("/queryOperatorForEmployee")
    public ResultVO queryOperatorForEmployee(){
        return ResultVO.success("查询成功",acOperatorService.queryOperatorForEmployee());
    }

    @ApiOperation(value = "判断新增时用户名是否重复",notes = "传登录用户名(userId)作为参数")
    @PostMapping("/existAddUserId")
    public ResultVO existAddUserId(@RequestBody @Validated @ApiParam(name = "AcOperatorUserIdRequest",value =
            "传入参数的AcOperatorUserIdRequest",required = true) AcOperatorUserIdRequest request){
        return ResultVO.success("查询成功",acOperatorService.existAddUserId(request.getUserId()));
    }

    @ApiOperation(value = "判断新增时用户名是否重复",notes = "传登录用户名(userId)和GUID(guid)作为参数")
    @PostMapping("/existUpdateUserId")
    public ResultVO existUpdateUserId(@RequestBody @Validated @ApiParam(name = "AcOperatorUserIdRequest",value =
            "传入参数的AcOperatorUserIdRequest",required = true) AcOperatorUserIdRequest request){
        return ResultVO.success("查询成功",acOperatorService.existUpdateUserId(request.getUserId(),request.getGuid()));
    }


    /** 下面为操作员状态变更接口 */

    @ApiOperation(value = "启用操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="启用操作员")
    @PutMapping("/startOperatorStatus")
    public ResultVO startOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("启用成功!",acOperatorQue);
    }

    @ApiOperation(value = "注销操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="注销操作员")
    @PutMapping("/cancelOperatorStatus")
    public ResultVO cancelOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("注销成功!",acOperatorQue);
    }

    @ApiOperation(value = "解锁操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="解锁操作员")
    @PutMapping("/deblockOperatorStatus")
    public ResultVO deblockOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("解锁成功!",acOperatorQue);
    }

    @ApiOperation(value = "重新启用操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="重新启用操作员")
    @PutMapping("/restartOperatorStatus")
    public ResultVO restartOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("重启成功!",acOperatorQue);
    }

    @ApiOperation(value = "签退操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="签退操作员")
    @PutMapping("/signOutOperatorStatus")
    public ResultVO signOutOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("签退成功!",acOperatorQue);
    }

    @ApiOperation(value = "签到/登陆操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="签到/登陆操作员")
    @PutMapping("/signInOperatorStatus")
    public ResultVO signInOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("登录成功!",acOperatorQue);
    }

    @ApiOperation(value = "恢复操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="恢复操作员")
    @PutMapping("/recoveryOperatorStatus")
    public ResultVO recoveryOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("恢复成功!",acOperatorQue);
    }

    @ApiOperation(value = "暂退操作员",notes = "传入AcOperatorStatusRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc ="暂退操作员")
    @PutMapping("/retreatOperatorStatus")
    public ResultVO retreatOperatorStatus(@RequestBody @Validated @ApiParam(value = "AcOperatorStatusRequest",name = "请求的AcOperatorStatusRequest类", required = true)  AcOperatorStatusRequest acOperatorStatusRequest){
        AcOperator acOperatorQue  = acOperatorService.selectById(acOperatorStatusRequest.getGuid());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        acOperatorQue = acOperatorService.changeOperatorStatus(acOperatorStatusRequest);
        return ResultVO.success("暂退成功!",acOperatorQue);
    }

}

